package esercitazione3.jUnitTest;

import org.junit.*;

import static org.junit.Assert.*;
import esercitazione3.exceptions.InvalidPositionException;
import esercitazione3.tda_BinaryTree.EmptyTreeException;
import esercitazione3.tda_Dictionary.Entry;
import esercitazione3.tda_Dictionary.InvalidKeyException;
import esercitazione3.tda_RBTree.RBNode;
import esercitazione3.tda_RBTree.RBTree;

public class RBTreeTest {

	private RBTree<Integer, String> tree;
	
	
	@Before
	public void setUp(){
		tree = new RBTree<Integer, String>();
	}
	
	@Test
	public void testInsertEqualValues() throws InvalidKeyException, EmptyTreeException{
		tree.insert(40, "Gianni");
		tree.insert(40, "Angelo");
		tree.insert(40, "Diego");
		RBNode<Entry<Integer, String>> root =(RBNode<Entry<Integer, String>>)tree.root();
		assertEquals(false, root.isRed());
		assertEquals(true, root.getLeft().isRed());
		assertEquals(true, root.getRight().isRed());
		// Le foglie devono essere tutte nere
		assertEquals(false, root.getLeft().getLeft().isRed());
		assertEquals(false, root.getLeft().getRight().isRed());
		assertEquals(false, root.getRight().getLeft().isRed());
		assertEquals(false, root.getRight().getRight().isRed());
	}
	
	
	@Test
	public void testInsert() throws InvalidKeyException, EmptyTreeException{
		tree.insert(40, "Gianni");
		tree.insert(29, "Angelo");
		tree.insert(59, "Diego");
		tree.insert(12, "Piero");
		RBNode<Entry<Integer, String>> root =(RBNode<Entry<Integer, String>>)tree.root();
		assertEquals(false, root.isRed());
		assertEquals(false, root.getLeft().isRed());
		assertEquals(true, root.getLeft().getLeft().isRed());
		assertEquals(false, root.getRight().isRed());
		// Le foglie devono essere tutte nere
		assertEquals(false, root.getLeft().getRight().isRed());
		assertEquals(false, root.getLeft().getLeft().getLeft().isRed());
		assertEquals(false, root.getLeft().getLeft().getRight().isRed());
		assertEquals(false, root.getRight().getLeft().isRed());
		assertEquals(false, root.getRight().getRight().isRed());
		
	}
	
	@Test
	public void testRoot() throws InvalidKeyException, InvalidPositionException, EmptyTreeException{
		tree.insert(3, "gianni");
		RBNode<Entry<Integer,String>> node = (RBNode<Entry<Integer, String>>) tree.root();
		assertEquals("gianni", node.element().getValue());
		assertEquals(false, node.isRed());
	}
	
	@Test
	public void testSize() throws InvalidKeyException{
		assertEquals(0, tree.size());
		tree.insert(40, "gianni");
		tree.insert(12, "gianni");
		tree.insert(33, "gianni");
		tree.insert(56, "gianni");
		tree.insert(45, "gianni");
		tree.insert(58, "gianni");
		tree.insert(10, "gianni");
		tree.insert(35, "gianni");
		tree.insert(60, "gianni");
		assertEquals(9, tree.size());	
			
	}

}
